import sys
sys.path.append("D:\\Day\\p2p")
import pytest
import requests

from api.api_approve_trust import ApiApproveTrust
from api.api_register_login import ApiRegisterLogin
from tools import GetLog, common_assert, read_json, read_html

logger = GetLog.get_logger()
class TestApproveTrust:
    def setup_method(self):
        # 获取session对象
        self.session = requests.session()
        # 获取ApiApproveTrust对象
        self.api_appr_trust = ApiApproveTrust(self.session)
        # 获取ApiRegisterLogin对象并调用登录接口
        ApiRegisterLogin(self.session).api_login()

    def teardown_method(self):
        self.session.close()
        logger.info("关闭session对象成功")
    # 1、认证接口测试
    @pytest.mark.parametrize("realname,card_id,expect_json",read_json("approve_trust.json","approve"))
    def test01_approve(self,realname,card_id,expect_json):
        try:
            resp = self.api_appr_trust.api_approve(realname,card_id)
            logger.info("认证接口测试结果为:{}".format(resp.json()))
            common_assert(resp,expect_json=expect_json)
            logger.info("认证接口断言成功")
        except Exception as e:
            logger.error(e)
            raise

    # 2、查询认证状态接口测试
    @pytest.mark.parametrize("expect_text",read_json("approve_trust.json","approve_status")[0])
    def test02_approve_status(self,expect_text):
        try:
            resp = self.api_appr_trust.api_approve_status()
            logger.info("查询认证状态测试结果为:{}".format(resp.text))
            common_assert(resp,expect_text=expect_text)
            logger.info("查询认证状态断言成功")
        except Exception as e:
            logger.error(e)
            raise

    # 3、后台开户请求接口测试
    @pytest.mark.parametrize("expect_text",read_json("approve_trust.json","trust")[0])
    def test03_trust(self,expect_text,expect_text2="OK"):
        try:
            resp = self.api_appr_trust.api_trust()
            logger.info("后台开户请求测试结果为:{}".format(resp.text))
            common_assert(resp,expect_text=expect_text)
            logger.info("后台开户请求断言成功")
            data = read_html(resp)
            logger.info("三方开户数据为:{}".format(data))
            result = self.session.post(url=data[0],data=data[1])
            logger.info("三方开户请求测试结果为:{}".format(resp.text))
            common_assert(result, expect_text=expect_text2)
            logger.info("三方开户请求断言成功")
        except Exception as e:
            logger.error(e)
            raise
